// src/utils/localStore.js
const fs = require('fs');
const path = require('path');
const os = require('os');

const storePath = path.join(os.homedir(), '.my-electron-vue-store.json');

// 读取
function readStore() {
  try {
    const data = fs.readFileSync(storePath, 'utf8');
    return JSON.parse(data);
  } catch (e) {
    return {};
  }
}

// 写入
function writeStore(data) {
  fs.writeFileSync(storePath, JSON.stringify(data, null, 2));
}

// 设置键值
function setItem(key, value) {
  const store = readStore();
  store[key] = value;
  writeStore(store);
}

// 获取键值
function getItem(key) {
  const store = readStore();
  return store[key];
}

// 删除
function removeItem(key) {
  const store = readStore();
  delete store[key];
  writeStore(store);
}

// 清空
function clear() {
  writeStore({});
}

module.exports = {
  setItem,
  getItem,
  removeItem,
  clear
};
